using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ThreadExercise.methods
{
    internal class Counter
    {
        private int _count = 0;
        private readonly object _lock = new object();
        public void Increment()
        {
            //需要确保多个线程使用的对象的_lock是同一个对象，否则会出现资源抢夺问题。
            lock (_lock)
            {
                _count++;
                Console.WriteLine($"线程({Thread.GetCurrentProcessorId()})-当前计数: {_count}");
                Thread.Sleep(500);
            }
        }
    }
}
